package andestours.interfaz;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import andestours.mundo.AndesTours;
import andestours.mundo.DestinoValue;
import andestours.mundo.ServicioValue;


public class ServletEstadia extends ServletTemplate {

	private static final long serialVersionUID = 1L;
	
	private int dinero;
	private int diaIda;
	private int mesIda;
	private int anoIda;
	private int diaVuelta;
	private int mesVuelta;
	private int anoVuelta;
	private String ida;
	private String vuelta;
	
	private String[] mes;

	@Override
	public void imprimirContenido(HttpServletRequest request, PrintWriter out) {
		
		if(request.getParameter("codigo") == null) {
			out.println("<br /><br /><br />Ha ocurrido un error. Por favor vuelva al inicio e intente de nuevo.");
			return;
		}
		
		mes = new String[13];
		mes[1] = "enero";
		mes[2] = "febrero";
		mes[3] = "marzo";
		mes[4] = "abril";
		mes[5] = "mayo";
		mes[6] = "junio";
		mes[7] = "julio";
		mes[8] = "agosto";
		mes[9] = "septiembre";
		mes[10] = "octubre";
		mes[11] = "noviembre";
		mes[12] = "diciembre";
		
		int codDestino = Integer.parseInt(request.getParameter("codigo"));
		
		boolean consulto = request.getParameter("dinero") != null;
		boolean selecciono = request.getParameter("servicios") != null;
		
		ArrayList<ServicioValue> servicios = new ArrayList<ServicioValue>();
		ServicioValue servicio = new ServicioValue();
		DestinoValue destino = AndesTours.getInstance().darDestino(codDestino);
		
		if(consulto || selecciono) {
			dinero = Integer.parseInt(request.getParameter("dinero"));
			diaIda = Integer.parseInt(request.getParameter("diaIda"));
			mesIda = Integer.parseInt(request.getParameter("mesIda"));
			anoIda = Integer.parseInt(request.getParameter("anoIda"));
			diaVuelta = Integer.parseInt(request.getParameter("diaVuelta"));
			mesVuelta = Integer.parseInt(request.getParameter("mesVuelta"));
			anoVuelta = Integer.parseInt(request.getParameter("anoVuelta"));
			ida = diaIda + "-" + mesIda + "-" + anoIda;
			vuelta = diaVuelta + "-" + mesVuelta + "-" + anoVuelta;
			Date inicio = new Date();
			servicios = AndesTours.getInstance().darServicios(codDestino, dinero, diaIda, mesIda, anoIda, diaVuelta, mesVuelta, anoVuelta);
			Date fin = new Date();
			System.out.println("RF2 Dar servicios: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
			
		}
		
		if(selecciono) {
			Date inicio = new Date();
			servicio = AndesTours.getInstance().darServicio(request.getParameter("servicios"));
			Date fin = new Date();
			System.out.println("RF2 Mostrar servicio: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
		}
		
		out.println("    <tr> ");
		out.println("        <td width='35%' bgcolor='#6666aa' valign='baseline'><center> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#FF9966'><b>2. Alojamiento y Alimentaci&oacute;n</b></font> <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <form action='estadia.html' method='post' onsubmit='return validarEstadia()'> ");
		out.println("                    <font face='Verdana, Arial, Helvetica, sans-serif'>Dinero disponible <br /> ");
		out.println("                    $ </font> ");
		out.println("                    <input type='text' maxlength='9' size='8' id='dinero' name='dinero' value='" + (consulto || selecciono ? dinero : "") + "' /> ");
		out.println("                    <br /> ");
		out.println("                    <br /> ");
		out.println("                    <br /> ");
		out.println("                    <font face='Verdana, Arial, Helvetica, sans-serif'>Fecha de ida <br /> ");
		out.println("                    <select id='diaIda' name='diaIda'> ");
		for(int i=1; i<=31; i++)
			out.println("						<option name='" + i + "' " + (consulto || selecciono ? diaIda == i ? "selected='selected'": "" : "") + " >" + i + "</option>");
		out.println("                    </select> / ");
		out.println("                    <select id='mesIda' name='mesIda'> ");
		for(int i=1; i<=12; i++)
			out.println("						<option value='" + i + "' " + (consulto || selecciono ? mesIda == i ? "selected='selected'": "" : "") + " >" + mes[i] + "</option>");
		out.println("                    </select> / ");
		out.println("                    <select id='anoIda' name='anoIda'> ");
		for(int i=2009; i<=2038; i++)
			out.println("						<option name='" + i + "' " + (consulto || selecciono ? anoIda == i ? "selected='selected'": "" : "") + " >" + i + "</option>");
		out.println("                    </select><br /> ");
		out.println("                    <br /> ");
		out.println("                    Fecha de vuelta <br /> ");
		out.println("                    <select id='diaVuelta' name='diaVuelta'> ");
		for(int i=1; i<=31; i++)
			out.println("						<option name='" + i + "' " + (consulto || selecciono ? diaVuelta == i ? "selected='selected'": "" : "") + " >" + i + "</option>");
		out.println("                    </select> / ");
		out.println("                    <select id='mesVuelta' name='mesVuelta'> ");
		for(int i=1; i<=12; i++)
			out.println("						<option value='" + i + "' " + (consulto || selecciono ? mesVuelta == i ? "selected='selected'": "" : "") + " >" + mes[i] + "</option>");
		out.println("                    </select> / ");
		out.println("                    <select id='anoVuelta' name='anoVuelta'> ");
		for(int i=2009; i<=2038; i++)
			out.println("						<option name='" + i + "' " + (consulto || selecciono ? anoVuelta == i ? "selected='selected'": "" : "") + " >" + i + "</option>");
		out.println("                    </select><br /><br /><br /> ");
		out.println("                    <br /> ");		
		out.println("			  <input type='hidden' id='codigo' name='codigo' value='" + codDestino + "' />");
		out.println("			  <input type='hidden' id='ida' name='ida' value='" + (consulto || selecciono ?  ida : "") + "' />");
		out.println("			  <input type='hidden' id='vuelta' name='vuelta' value='" + (consulto || selecciono ?  ida : "") + "' />");
		out.println("			  <input type='submit' value='Buscar' /> ");
		out.println("			  </form>");
		out.println("                    <br /> ");
		out.println("                    <br /> ");
		out.println("                    <br /> ");
		out.println("                    Servicios</font><br /> ");
		out.println("          	  <form action='estadia.html' method='post'> ");
		out.println("			  <input type='hidden' id='codigo' name='codigo' value='" + codDestino + "' />");
		out.println("			  <input type='hidden' id='dinero' name='dinero' value='" + dinero + "' />");
		out.println("			  <input type='hidden' id='diaIda' name='diaIda' value='" + diaIda + "' />");
		out.println("			  <input type='hidden' id='mesIda' name='mesIda' value='" + mesIda + "' />");
		out.println("			  <input type='hidden' id='anoIda' name='anoIda' value='" + anoIda + "' />");
		out.println("			  <input type='hidden' id='diaVuelta' name='diaVuelta' value='" + diaVuelta + "' />");
		out.println("			  <input type='hidden' id='mesVuelta' name='mesVuelta' value='" + mesVuelta + "' />");
		out.println("			  <input type='hidden' id='anoVuelta' name='anoVuelta' value='" + anoVuelta + "' />");
		out.println("			  <input type='hidden' id='ida' name='ida' value='" + ida + "' />");
		out.println("			  <input type='hidden' id='vuelta' name='vuelta' value='" + vuelta + "' />");
		out.println("             <select name='servicios' id='servicios' " + (consulto || selecciono ? "" : "disabled='disabled'") + " /> ");
		out.println("             <option value='0' onclick=\"javascript:document.forms[2].boton.disabled='disabled'\">--- Seleccione ---</option> ");
		
		for(int i=0; i<servicios.size(); i++) {
			boolean sel = selecciono ? servicios.get(i).getCodigo().equals(servicio.getCodigo()) : false;
			out.println("                    <option value='" + servicios.get(i).getCodigo() + "' onclick='javascript:escogerServicio()' " + (sel ? "selected='selected'" : "") + ">" + servicios.get(i).getNombre() + "</option> ");
		}
			
		out.println("                    </select> ");
		out.println("					 </form>");		
		out.println("           	     <br /> ");
		out.println("          	  	     <br /> ");
		out.println("          		     <br /> ");
		out.println("					 <form action='transporte.html' method='post'> ");
		out.println("					 <input type='hidden' id='destino' name='destino' value='" + codDestino + "' /> ");
		out.println("					 <input type='hidden' id='servicio' name='servicio' value='" + (selecciono ? servicio.getCodigo() : 0) + "' /> ");
		out.println("				 	 <input type='hidden' id='ida' name='ida' value='" + ida + "' />");
		out.println("				 	 <input type='hidden' id='vuelta' name='vuelta' value='" + vuelta + "' />");
		out.println("                    <input id='boton' name='boton' type='submit' value='Continuar' " + (selecciono ? "" : "disabled='disabled'") + " /> ");

		out.println("                </form> ");
		out.println("            </center></td> ");
		out.println("        <td valign='baseline'><center> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + destino.getNombre() + "</font> <br /> ");
		out.println("                <font face='Times New Roman, Times, serif'>" + destino.getPais() + " - " + destino.getContinente() + "</font> <br /> ");
		out.println("                <img border='2' height='150' src='" + destino.getFoto() + "' /> <br /> ");
		out.println("                <br /><br /><br /><br /> ");

		if(selecciono) {
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>Hotel " + servicio.getHotel() + "</font> <br /> ");
			for(int i=0; i<servicio.getTipo(); i++)
				out.println("&#10029;");
			out.println("<br />Habitaciones: " + servicio.getHabitaciones());
			out.println("<br />" + (servicio.getCupos() <= 0 ? "<font color='#ff0000'>" : "<font>") + "Disponibles: " + servicio.getCupos() + "</font>");
			out.println("<br /><br />Restaurante <b>" + servicio.getRestaurante() + "</b>");
			out.println("				<br /><br /><div align='justify'> ");
			out.println("				<font face='Times New Roman, Times, serif'>Costo del hotel: $ " + servicio.getCostoH() + " / d&iacute;a<br />Costo del restaurante: $ " + servicio.getCostoR() + " / d&iacute;a<br />Costo total: $ " + ( servicio.getCostoH() + servicio.getCostoR() ) + " / d&iacute;a<br /> ");
			out.println("				</font></div> ");
		}
		
		out.println("		</center>");
		out.println("		</td>");
		out.println("	</tr>");
	}

}
